<?php
namespace App\Http\Controllers\Admin;

use App\DBs\Admin\TongjiDB;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class IndexController extends BaseController
{
    public function __construct()
    {
        $this->dbTongji = new TongjiDB();
    }

    //主页统计数据
    public function getTongji(Request $request)
    {
        $res = $this->dbTongji->getTongji();
        return $this->toJson(0,'',$res['data']);
    }

    //大会介绍获取
    public function getDhIntro(Request $request)
    {
        $res = file_get_contents('dhjieshao.json');
        if (!$res) {
            $res = ['title'=>'大会介绍','img'=>'','detail'=>''];
        } else {
            $res = json_decode($res,true);
        }
        $res['detail'] = json_decode($res['detail'],true);
        return $this->toJson(0,'success',$res);
    }

    //大会介绍修改
    public function setDhIntro(Request $request)
    {
        $arys = $request->all();
        $img = isset($arys['img'])&&$arys['img'] ? $arys['img'] : '';
        $detail = isset($arys['detail'])&&$arys['detail'] ? $arys['detail'] : '';
        if (!$detail) {
            return $this->toJson(-2,'介绍未填写');
        }
        $res = ['title'=>'大会介绍','img'=>'','detail'=>''];
        if ($img) $res['img'] = $img;
        if ($detail) $res['detail'] = json_encode($detail,JSON_UNESCAPED_UNICODE);
        //$json = json_encode($res,[JSON_UNESCAPED_UNICODE,JSON_PRETTY_PRINT]);
        //$json = json_encode($res,384); //JSON_UNESCAPED_UNICODE+JSON_PRETTY_PRINT=>256+128=384
        //var_dump(JSON_UNESCAPED_UNICODE,JSON_PRETTY_PRINT); exit;
        $json = json_encode($res,384);
        file_put_contents('dhjieshao.json',$json);
        return $this->toJson(0);
    }

    //大会议程获取
    public function getAgenda(Request $request)
    {
        $res = file_get_contents('dhyicheng.json');
        if (!$res) {
            $res = ['title'=>'大会议程','img'=>''];
        } else {
            $res = json_decode($res,true);
        }
        return $this->toJson(0,'success',$res);
    }

    //大会议程修改
    public function setAgenda(Request $request)
    {
        $arys = $request->all();
        $img = isset($arys['img'])&&$arys['img'] ? $arys['img'] : '';
        if (!$img) {
            return $this->toJson(-2,'议程未上传');
        }
        $res = ['title'=>'大会议程','img'=>''];
        if ($img) $res['img'] = $img;
        //$json = json_encode($res,384); //JSON_UNESCAPED_UNICODE+JSON_PRETTY_PRINT=>256+128=384
        $json = json_encode($res,384);
        file_put_contents('dhyicheng.json',$json);
        return $this->toJson(0);
    }

    //学术列表
    public function getLearnIndex(Request $request)
    {
//        $arys = $request->all();
//        $meetid = isset($arys['meetid']) && $arys['meetid'] ? $arys['meetid'] : '';
        $res = $this->dbTongji->getLearnIndex();
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, 'success', $res['data']);
    }

    //学术新增
    public function setLearnAdd(Request $request)
    {
        $arys = $request->all();
        $title = isset($arys['title']) && $arys['title'] ? $arys['title'] : '';
        $detail = isset($arys['detail']) && $arys['detail'] ? $arys['detail'] : '';
        $img = isset($arys['img']) && $arys['img'] ? $arys['img'] : '';
        if (!$title || !$detail || !$img) {
            return $this->toJson(-2, '参数不全');
        }
        $detail = json_encode($detail,JSON_UNESCAPED_UNICODE);
        $upData = ['title'=>$title,'detail'=>$detail];
        if ($img) $upData['img'] = Upload::getPathByBase64($img);
        $res = $this->dbTongji->setLearnAdd($upData);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //学术列表
    public function getLearnOne(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id']) && $arys['id'] ? $arys['id'] : '';
        $res = $this->dbTongji->getLearnOne($id);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, 'success', $res['data']);
    }

    //学术新增
    public function setLearnUpdate(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id']) && $arys['id'] ? $arys['id'] : '';
        $title = isset($arys['title']) && $arys['title'] ? $arys['title'] : '';
        $detail = isset($arys['detail']) && $arys['detail'] ? $arys['detail'] : '';
        $img = isset($arys['img']) && $arys['img'] ? $arys['img'] : '';
        if (!$id || !$title || !$detail) {
            return $this->toJson(-2, '参数不全');
        }
        $detail = json_encode($detail,JSON_UNESCAPED_UNICODE);
        $upData = ['title'=>$title,'detail'=>$detail];
        if ($img) $upData['img'] = Upload::getPathByBase64($img);
        $res = $this->dbTongji->setLearnUpdate($id,$upData);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //主题删除
    public function setLearnDel(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id']) && $arys['id'] ? $arys['id'] : '';
        if (!$id) {
            return $this->toJson(-2, '没有参数');
        }
        $res = $this->dbTongji->setLearnDel($id);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //组织架构获取
    public function getZzjg(Request $request)
    {
        $res = file_get_contents('dhzzjg.json');
        if (!$res) {
            $res = ['cn'=>'组织架构','img'=>''];
        } else {
            $res = json_decode($res,true);
        }
        return $this->toJson(0,'success',$res);
    }

    //组织架构修改
    public function setZzjg(Request $request)
    {
        $arys = $request->all();
        $img = isset($arys['img'])&&$arys['img'] ? $arys['img'] : '';
        if (!$img) {
            return $this->toJson(-2,'未上传');
        }
        $res = ['cn'=>'组织架构','img'=>$img];
        $json = json_encode($res,384);
        file_put_contents('dhzzjg.json',$json);
        return $this->toJson(0);
    }

    //影像馆获取
    public function getYxgIndex(Request $request)
    {
        $res = $this->dbTongji->getYxgIndex();
        if ($res['code']!=0){
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0,'success',$res['data']);
    }

    //影像馆新增
    public function setYxgAdd(Request $request)
    {
        $arys = $request->all();
        $img = isset($arys['img']) && $arys['img'] ? $arys['img'] : '';
        if (!$img) {
            return $this->toJson(-2,'没有图片上传');
        }
        $img = Upload::getPathByBase64($img);
        $upData = ['type'=>3,'img'=>$img];
        $res = $this->dbTongji->setYxgAdd($upData);
        if ($res['code']!=0){
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0);
    }

    //影像馆删除
    public function setYxgDel(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id']) && $arys['id'] ? $arys['id'] : '';
        if (!$id) {
            return $this->toJson(-2,'没有图片');
        }
        $res = $this->dbTongji->setYxgDel($id);
        if ($res['code']!=0){
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0);
    }

    //联系我们获取
    public function getLianxi(Request $request)
    {
        $res = file_get_contents('dhlianxi.json');
        if (!$res) {
            $res = [
                'cn'=>'联系我们',
                'gscn'=>'',
                'gsen'=>'',
                'tel'=>'',
                'email'=>'',
                'dizhi01'=>'',
                'dizhi02'=>'',
                'bdlng'=>'',
                'bdlat'=>'',
            ];
        } else {
            $res = json_decode($res,true);
        }
        return $this->toJson(0,'success',$res);
    }

    //组织架构修改
    public function setLianxi(Request $request)
    {
        $arys = $request->all();
        $gscn = isset($arys['gscn'])&&$arys['gscn'] ? $arys['gscn'] : '';
        $gsen = isset($arys['gsen'])&&$arys['gsen'] ? $arys['gsen'] : '';
        $tel = isset($arys['tel'])&&$arys['tel'] ? $arys['tel'] : '';
        $email = isset($arys['email'])&&$arys['email'] ? $arys['email'] : '';
        $dz01 = isset($arys['dz01'])&&$arys['dz01'] ? $arys['dz01'] : '';
        $dz02 = isset($arys['dz02'])&&$arys['dz02'] ? $arys['dz02'] : '';
        $lng = isset($arys['lng'])&&$arys['lng'] ? $arys['lng'] : '';
        $lat = isset($arys['lat'])&&$arys['lat'] ? $arys['lat'] : '';
//        if (!$gscn || !$gsen || !$tel || !$email || !$dz01 || !$dz02 || !$lng || !$lat) {
//            return $this->toJson(-2,'未上传');
//        }
        if (!$gscn || !$gsen) return $this->toJson(-2,'公司名未填写');
        if (!$tel) return $this->toJson(-2,'座机未填写');
        if (!$email) return $this->toJson(-2,'邮箱未填写');
        if (!$dz01 || !$dz02) return $this->toJson(-2,'地址未填写');
        if (!$lng || !$lat) return $this->toJson(-2,'坐标未填写');
        $res = ['cn'=>'联系我们',
            'gscn'=>$gscn, 'gsen'=>$gsen,
            'tel'=>$tel, 'email'=>$email,
            'dizhi01'=>$dz01, 'dizhi02'=>$dz02,
            'lng'=>$lng, 'lat'=>$lat,
        ];
        $json = json_encode($res,384);
        file_put_contents('dhlianxi.json',$json);
        return $this->toJson(0);
    }
}